package com.cx.service.serviceimp;

import com.cx.utils.ExcelUtils;
import com.cx.utils.UtilsFields;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import com.cx.entity.ZtCustomer;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;

import com.cx.mapper.ZtCustomerMapper;
import com.cx.service.ZtCustomerService;

@Service
public class ZtCustomerServiceImpl implements ZtCustomerService {



    @Resource
    private ZtCustomerMapper ztCustomerMapper;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return ztCustomerMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(ZtCustomer record) {
        return ztCustomerMapper.insert(record);
    }

    @Override
    public int insertOrUpdate(ZtCustomer record) {
        return ztCustomerMapper.insertOrUpdate(record);
    }

    @Override
    public int insertOrUpdateSelective(ZtCustomer record) {
        return ztCustomerMapper.insertOrUpdateSelective(record);
    }

    @Override
    public int insertSelective(ZtCustomer record) {
        return ztCustomerMapper.insertSelective(record);
    }

    @Override
    public ZtCustomer selectByPrimaryKey(Integer id) {
        return ztCustomerMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(ZtCustomer record) {
        return ztCustomerMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(ZtCustomer record) {
        return ztCustomerMapper.updateByPrimaryKey(record);
    }

    @Override
    public int updateBatch(List<ZtCustomer> list) {
        return ztCustomerMapper.updateBatch(list);
    }

    @Override
    public int batchInsert(List<ZtCustomer> list) {
        return ztCustomerMapper.batchInsert(list);
    }

    @Override
    public List<ZtCustomer> list(ZtCustomer ztCustomer) {
//      重试机制
        int maxAttempts = 5;  //最大重试次数

        int attempts = 0;// 当前重试次数

        List<ZtCustomer> list = null;

        while (attempts < maxAttempts) {
            try {
                int offerSet = (ztCustomer.getCurrentPage() - 1) * ztCustomer.getPageSize();
                ztCustomer.setOffSet(offerSet);
                int count = ztCustomerMapper.count(ztCustomer);
                list = ztCustomerMapper.list(ztCustomer);
                if (list != null && !list.isEmpty()) {
                    list.get(0).setTotal(count);
                    list.stream().forEach(item -> {
                        if (item.getSex() != null) {
                            if (item.getSex() == 0) {
                                item.setSexChar("男");
                            }

                            if (item.getSex() == 1) {
                                item.setSexChar("女");
                            }


                        }
                    });
                    break;
                }
            } catch (Exception e) {
                System.out.println("第" + (attempts + 1) + "次查询客户错误异常：" + e);
            }
            attempts++;
            if (attempts < maxAttempts) {
                try {
                    //等待五百毫秒
                    Thread.sleep(1000);
                } catch (Exception e) {
                    System.out.println("查询客户等待错误异常：" + e);
                }
            }
        }

        return list;
    }

    @Override
    public void add(ZtCustomer ztCustomer) {
        try {
            if (ztCustomer.getId() == null) {
                ztCustomerMapper.insertSelective(ztCustomer);
            } else {
                ztCustomerMapper.updateByPrimaryKeySelective(ztCustomer);
            }

        } catch (Exception e) {
            System.out.println("新增客户错误=======" + e);

        }

    }


    @Override
    public int count(ZtCustomer ztCustomer) {
        return ztCustomerMapper.count(ztCustomer);
    }

    @Override
    public int deleteCustomer(List<Integer> ids) {
        return ztCustomerMapper.deleteCustomer(ids);
    }

    //    下载客户模板
    @Override
    public ResponseEntity<InputStreamResource> template() throws IOException {

        return null;

    }


}





